package com.hello.world.http.lighthouse;

import com.google.gson.Gson;
import com.hello.world.http.lighthouse.data.CmOrgStructureDTO;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaozeyang
 * @date 2018/11/30 11:41
 * @description
 */
public class StuCityAdd {

    static Gson gson = new Gson();
    static CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    static List<CmOrgStructureDTO> list = new ArrayList<CmOrgStructureDTO>() {{
        add(new CmOrgStructureDTO("全国在线", "0101"));
        add(new CmOrgStructureDTO("淮安", "0517"));
        add(new CmOrgStructureDTO("邯郸", "0310"));
        add(new CmOrgStructureDTO("唐山", "0315"));
        add(new CmOrgStructureDTO("潍坊", "0536"));
        add(new CmOrgStructureDTO("临沂", "0539"));
        add(new CmOrgStructureDTO("银川", "0951"));
        add(new CmOrgStructureDTO("南宁", "0771"));
        add(new CmOrgStructureDTO("昆明", "0871"));
        add(new CmOrgStructureDTO("海口", "0898"));
        add(new CmOrgStructureDTO("呼和浩特", "0471"));
        add(new CmOrgStructureDTO("乌鲁木齐", "0991"));
    }};

    public static void main(String[] args) throws IOException {
        for (CmOrgStructureDTO cmOrgStructureDTO : list) {
            cmOrgStructureDTO.setDepartId("R2_01_".concat(cmOrgStructureDTO.getCityCode()));
            cmOrgStructureDTO.setLevel(0);
            cmOrgStructureDTO.setStatus(1);
            cmOrgStructureDTO.setParentId(2L);
            cmOrgStructureDTO.setSystem(1);
            HttpPost httpPost = getHttpPost(cmOrgStructureDTO);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            System.out.println(EntityUtils.toByteArray(response.getEntity()));
        }
    }

    private static HttpPost getHttpPost(CmOrgStructureDTO cmOrgStructureDTO) {
        HttpPost httpPost = new HttpPost("http://lighthouse-test.xesapp.com/api/cm-org-structures");
        Map<String, String> headerMap = new HashMap<String, String>() {{
            put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzIiwiYXV0aCI6Im51bGwiLCJleHAiOjE1NDM4OTE2Njh9.l_FJcQEVvv-Ap5csJlH2DGx452-y8-ruYvgXMdkuGSsSOwsQwJsLGKvGFm7gUvRJpWa93deFOeVTvvDR15pRbg");
            put("Content-Type", "application/json");
        }};
        headerMap.forEach(httpPost::addHeader);
        String bodyStr = gson.toJson(cmOrgStructureDTO);
        httpPost.setEntity(new StringEntity(bodyStr, "UTF-8"));
        return httpPost;
    }
}
